package com.example.hanlp_service.controller;

import com.example.hanlp_service.dto.DependencyWordDTO;
import com.example.hanlp_service.dto.NamedEntityDTO;
import com.hankcs.hanlp.seg.common.Term;
import org.springframework.web.bind.annotation.*;
import com.example.hanlp_service.utils.HanLPUtil;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/")
public class AllInOneController {

    // ======== Summary API ========
    @PostMapping("/api/summary")
    public List<String> generateSummary(@RequestBody TextRequest request) {
        if (request.getSize() != null && request.getSize() > 0) {
            return HanLPUtil.extractSummary(request.getText(), request.getSize());
        } else {
            return HanLPUtil.extractSummary(request.getText()); // 智能分析
        }
    }

    // ======== NER API ========
    @PostMapping("/api/ner")
    public List<NamedEntityDTO> extractEntities(@RequestBody TextRequest request) {
        return HanLPUtil.recognizeNamedEntities(request.getText());
    }

    // ======== Dependency Parsing API ========
    @PostMapping("/api/dependency")
    public List<DependencyWordDTO> parseDependency(@RequestBody TextRequest request) {
        return HanLPUtil.dependencyParse(request.getText());
    }

    // ======== Phrase Extraction API ========
    @PostMapping("/api/phrase")
    public List<String> extractPhrases(@RequestBody PhraseRequest request) {
        String text = request.getText();
        Integer count = request.getCount();

        if (count != null && count > 0) {
            return HanLPUtil.extractPhrase(text, count);
        } else {
            return HanLPUtil.extractPhrase(text);
        }
    }

    // ======== Segmentation APIs ========
    @GetMapping("/api/segment/analyze")
    public List<String> analyzeText(@RequestParam String text) {
        List<Term> terms = HanLPUtil.segment(text);
        return terms.stream()
                .map(term -> term.word)
                .collect(Collectors.toList());
    }

    @GetMapping("/api/segment/nlp/segment")
    public List<String> nlpSegment(@RequestParam String text) {
        return HanLPUtil.nlpSegmentWords(text);
    }

    @GetMapping("/api/segment/nlp/analyze")
    public String nlpAnalyze(@RequestParam String text) {
        return HanLPUtil.analyzeWithPos(text);
    }

    @GetMapping("/api/segment/index/words")
    public List<String> indexSegmentWords(@RequestParam String text) {
        return HanLPUtil.indexSegmentWords(text);
    }

    @GetMapping("/api/segment/index/analyze")
    public List<String> indexSegmentAnalyze(@RequestParam String text) {
        return HanLPUtil.indexSegmentWithPosition(text);
    }

    @GetMapping("/api/segment/nshort/words")
    public List<String> nShortSegmentWords(@RequestParam String text) {
        return HanLPUtil.nShortSegmentWords(text);
    }

    @GetMapping("/api/segment/nshort/analyze")
    public List<String> nShortSegmentAnalyze(@RequestParam String text) {
        return HanLPUtil.nShortSegmentWithPos(text);
    }

    @GetMapping("/api/segment/shortest/words")
    public List<String> shortestSegmentWords(@RequestParam String text) {
        return HanLPUtil.shortestSegmentWords(text);
    }

    @GetMapping("/api/segment/shortest/analyze")
    public List<String> shortestSegmentAnalyze(@RequestParam String text) {
        return HanLPUtil.shortestSegmentWithPos(text);
    }

    @GetMapping("/api/keyword")
    public List<String> extractKeywords(@RequestParam String text,
            @RequestParam(required = false) Integer count) {
        if (count != null && count > 0) {
            return HanLPUtil.extractKeyword(text, count);
        } else {
            return HanLPUtil.extractKeyword(text); // 智能提取
        }
    }

    // ======== Inner Classes for Request DTOs ========

    public static class TextRequest {
        private String text;
        private Integer size; // 可选参数

        public String getText() {
            return text;
        }

        public void setText(String text) {
            this.text = text;
        }

        public Integer getSize() {
            return size;
        }

        public void setSize(Integer size) {
            this.size = size;
        }
    }

    public static class PhraseRequest {
        private String text;
        private Integer count;

        public String getText() {
            return text;
        }

        public void setText(String text) {
            this.text = text;
        }

        public Integer getCount() {
            return count;
        }

        public void setCount(Integer count) {
            this.count = count;
        }
    }
}